/**
 * @author uu
 * @file 开始页面控制
 */
cc.Class({
  extends: cc.Component,
  properties: {
    // bannerNode: cc.Node,
    // labelNode: cc.Node,
    tipView: cc.Node,
    loadProgress: cc.ProgressBar,
    descText: cc.Label,
    socket: require('socketManager'),
    adsManager: require('adsManager'),
    redBag: require('redBagData')
  },
  start() {
    this.isDeBug = true;
    this._json = {};
    this.tipView.active = false;
    this.Test();
    // if(cc.sys.localStorage.getItem('agree') == 0) {
    //     this.tipView.active = false;
    //     this.Test();
    // }
    // else {
    //     this.tipView.active = true;
    // }
  },

  onEnable () {
    cc.director.on('Register', this.onRegisterHandler, this);
    cc.director.on('PostInit', this.onPostInitHandler, this);
    cc.director.on('postLoginRecord', this.onPostLoginRecordHandler, this)
  },

  onDisable () {
    cc.director.off('Register', this.onRegisterHandler, this);
    cc.director.off('PostInit', this.onPostInitHandler, this);
    cc.director.off('postLoginRecord', this.onPostLoginRecordHandler, this)
  },

  onRegisterHandler() {
    this.loadProgress.progress = 0.1;
    this.descText.string = "初始化信息";
    this.socket.postInitHandler((res)=>{
        cc.director.emit('PostInit', true);
    })
},

  onPostInitHandler() {
      this.loadProgress.progress = 0.6;
      this.descText.string = "初始化成功";
      this.socket.postLoginRecordHandler(()=>{
          cc.director.emit('postLoginRecord', true);
      })
  },

  onPostLoginRecordHandler() {
      this.loadProgress.progress = 1;
      this.descText.string = "登录游戏";
      this.redBag.requestConfigs(()=>{
        cc.director.emit('StartGame', true);
        //cc.director.loadScene('scene');
          // this.pageMgr.onOpenPage(1)
          // this.game.gameStart()
      })
  },


  btnOkClick() {
      if(!this.isDeBug) {

      }
      else {
          this.tipView.active = false;
          this.Test();
          cc.sys.localStorage.setItem('agree', 0);
      }
  },

  Test(){
    console.log('游戏配置获取');
      // if(cc.sys.localStorage.getItem('testInfo')) {
      //     this._json = JSON.parse(cc.sys.localStorage.getItem('testInfo'));
      //     //var xhr = cc.loader.getXMLHttpRequest();
      // }
      // else {
      //     this._json["plan_id"] = "540056";
      //     this._json["package_id"] = "";
      //     this._json["device_model"] = "Test " + Math.ceil(Math.random() * 100000);
      //     this._json["device_system"] = "29";
      //     this._json["androidId"] = "444db12299b21" + Math.ceil(Math.random() * 99999);
      //     this._json["package_version"] = "0.1";
      //     this._json["package_identifier"] = "com.dgxxl";
      //     this._json["imei"] = " ";
      //     this._json["oaid"] = " ";
      //     this._json["mac"] = "" ;
      //     this._json['device_uuid'] = '444db12299b21'
      //     this._json["device_data"] = "20";
      //     this._json["tag"] = "";
      //     this._json['idfv'] = ' ';
      //     this._json['idfa'] = ' ';
      //     this._json['openid'] = '100100100';
      //     this._json['x'] = '1';
      //     this._json['y'] = '2';
          
      //     cc.sys.localStorage.setItem('testInfo', JSON.stringify(this._json));
      // }
      //cc.director.emit('StartGame', true);
      this.socket.register(this._json, (res)=>{
        console.log('获取游戏参数', res.hang)
          cc.sys.localStorage.setItem('hang', res.hang);
          cc.director.emit('StartGame', true);
          // cc.sys.localStorage.setItem('package_id', res.package_id);
          // this.adsManager.reloadData(res.ad_space_list);
          // cc.sys.localStorage.setItem('securityLevel', res.s_level);
         // cc.director.emit('Register', true);
      });
  },

  onTouched() {

  },
  showAnimation() {
    return new Promise((resolve, rejects) => {
      let action1 = cc.scaleTo(0.5, 0, 0).easing(cc.easeBackIn())
      let action2 = cc.blink(0.5, 3)
      //this.bannerNode.runAction(action1)
      let action = cc.sequence(action2, cc.callFunc(() => {
        resolve()
      }))
      //this.labelNode.runAction(action)
    })
  },
});